import axios from 'axios'
import metadata from '@/lib/script/metadata'

// let baseURL = '/api';
let baseURL = 'http://log.maysan.cn/';
// let baseURL = 'http://192.168.1.191:15000/';

const http = axios.create({
  baseURL,
  withCredentials: true,
  responseType: 'json',
  timeout: 15000,
})

http.interceptors.request.use(
  (config: any) => {
    console.log(config);
    if(!config.not_token){
      let token = metadata.getToken();
      token ? config.headers['Authorization'] = `Bearer ` + token : null;
    } else {
      delete config.not_token;
    }
    return config;
  },
  error => {
    return Promise.reject(error)
  }
)
http.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error.response.data);
  }
)

export default http